#include"function_w.h"

#define PATH_MLEN 1048         //路径最大长度

void mkDir(char *filename,int fd){

    char path[PATH_MLEN];
    getcwd(path, PATH_MLEN);

    printf("mkDir: path = %s\n", path);

    DIR *dir = opendir(path); //打开目录
    ERROR_CHECK(dir, NULL, "opendir");

    struct dirent *pdirent; //目录项指针
    while ((pdirent = readdir(dir)) != NULL)
    {
        if (strcmp(pdirent->d_name, filename) == 0 && pdirent->d_type == DT_DIR) //该文件夹已存在，则创建失败
        {
            char msg[128] = "mkdir faild,this dir has been created.\n";
            sendm(fd, msg, strlen(msg));
            closedir(dir);

            return 0;
        }
    }

    char filePath[PATH_MLEN] = {0};
    strcpy(filePath, path);
    strcat(filePath, "/");
    strcat(filePath, filename);

    printf("mkDir: filePath = %s\n", filePath);

    int ret = mkdir(filePath, 0777); //创建文件夹
    ERROR_CHECK(ret, -1, "mkdir");

    char msg[128] = "mkdir success.\n";
    sendm(fd, msg, strlen(msg));
    closedir(dir);

    return 0;
}